<!-- Page de détails d'un sous-domaine -->
<template>
  <main>
    <LayoutContainer>
      <UiLoadingPanel v-if="pending || activationPending" />
      <div>
        <div>
          {{ $t('youRegisteredTheFollowingSubdomain')}} :
        </div>

        <div class="pa-8">
          <b>{{ subdomain.subdomain }}</b>
          <span class="text-on-neutral">.opentalent.fr</span>
        </div>

        <div>
          <div v-if="subdomain.active">
            <v-icon class="text-success icon small mr-2">
              fa-solid fa-check
            </v-icon>
            {{ $t('subdomainIsCurrentlyActive') }}
          </div>
          <div v-else>
            {{ $t('doYouWantToActivateThisSubdomain') }} ?
          </div>
        </div>

        <div class="mt-6 d-flex flex-row">
          <v-btn class="mr-12" @click="quit">
            {{ $t('back') }}
          </v-btn>
          <div v-if="!subdomain.active">
            <v-btn color="primary" @click="activateAndQuit" >
              {{ $t('activate') }}
            </v-btn>
          </div>
        </div>
      </div>
    </LayoutContainer>
  </main>
</template>

<script setup lang="ts">
  import Subdomain from "~/models/Organization/Subdomain";
  import {useEntityFetch} from "~/composables/data/useEntityFetch";
  import {useOrganizationProfileStore} from "~/stores/organizationProfile";
  import {useEntityManager} from "~/composables/data/useEntityManager";

  const { em } = useEntityManager()
  const { fetch } = useEntityFetch()
  const organizationProfile = useOrganizationProfileStore()

  const router = useRouter()
  const route = useRoute()

  if (!route.params.id || isNaN(route.params.id as any)) {
    throw new Error('no id found')
  }
  const id: number = parseInt(route.params.id as string)

  const { data: subdomain, pending } = fetch(Subdomain, id)

  const activationPending: Ref<boolean> = ref(false)

  const activateAndQuit = () => {
    activationPending.value = true
    em.patch(Subdomain, id, { active: true} )
    quit()
  }

  const quit = () => {
    router.push('/parameters#website')
    activationPending.value = false
  }


</script>
